﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharePoint.Security.ConfigAnalyzer.Client.Views
{
    /// <summary>
    /// Interaction logic for Rules.xaml
    /// </summary>
    public partial class Rules : UserControl
    {
        public Rules()
        {
            InitializeComponent();
            viewModel = new ViewModels.RulesViewModel();
            LoadData();
        }

        private ViewModels.RulesViewModel viewModel;
        public void LoadData()
        {
            viewModel.FetchRules();
            ListCollectionView collection = new ListCollectionView(viewModel.Rules);
            collection.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));

            this.grdRules.DataContext = collection;
        }

        private void cmdScan_Click_1(object sender, RoutedEventArgs e)
        {
            pbStatus.IsIndeterminate = true;
            pbStatus.Visibility = System.Windows.Visibility.Visible;
            pbStatus.Value = 0;
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.DoWork += worker_DoWork;
            worker.ProgressChanged += worker_ProgressChanged;

            worker.RunWorkerAsync();


        }

        int increment = 0;

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var selectedRules = viewModel.Rules.Where(c => c.IsSelected);


            increment = 100 / selectedRules.Count();


            foreach (var rule in viewModel.Rules.Where(c => c.IsSelected))
            {
                try
                {
                    rule.RuleObj.Validate();
                    rule.CurrentValue = rule.RuleObj.CurrentValue;
                    rule.Status = rule.RuleObj.Status.ToString();
                }
                catch (Exception ex)
                {
                    rule.CurrentValue = string.Format("Error occurred while trying to fetch value. Error: {0}", ex.ToString());
                    rule.Status = "InConclusive";
                }

            }

            (sender as BackgroundWorker).ReportProgress(increment);


        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbStatus.IsIndeterminate = false;
            pbStatus.Visibility = System.Windows.Visibility.Hidden;
        }


        private void SelectAll_Checked(object sender, RoutedEventArgs e)
        {
            if (viewModel.Rules != null)
            {
                if (this.SelectAll.IsChecked == true)
                {
                    foreach (var item in viewModel.Rules)
                    {
                        item.IsSelected = true;
                    }
                }
                else
                {
                    foreach (var item in viewModel.Rules)
                    {
                        item.IsSelected = false;
                    }
                }

            }
        }

        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            if (viewModel.Rules != null)
            {
                if (this.SelectAll.IsChecked == true)
                {
                    foreach (var item in viewModel.Rules)
                    {
                        item.IsSelected = true;
                    }
                }
                else
                {
                    foreach (var item in viewModel.Rules)
                    {
                        item.IsSelected = false;
                    }
                }

            }
        }

        private void cmdExport_Click(object sender, RoutedEventArgs e)
        {
           try
            {
                var filePath = this.viewModel.ExportToExcel();
                MessageBox.Show("Data exported succesfully at " + Environment.NewLine + filePath, "Done!", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
